#include "mod_priv.h"

/* terminal module. */
#define APPLE_MODULE_NAME	"term"

#define PROMPT()	printf("Please Input Word: ")

int startdict_op()
{
	char ch, word[MAX_WORD_LENGTH];
	int len = -1;

redo:
	PROMPT();
redo2:
	/* gets() is not a safe one. */
	fgets(word, MAX_WORD_LENGTH, stdin);
	if (word[0] == 0)
		goto redo2;

	len = strlen(word);

	if (len == 1)
		goto redo;

	/* Since fgets will put '\n' into word. */
	word[len -1] = 0;
	len--;

	if (len == 1 && word[0] == 'q') {
		printf("Quit [Y/N]?");
		scanf("%c", &ch);
		if (ch == 'Y' || ch == 'y') {
			printf("Exit now...\n");	
			return -1;
		} else
			goto redo;	
	}
	/* dict_core api */
	get_word_explain(word, NULL);
	goto redo;
}

static int term_start(apple_module_t *m)
{	
	return startdict_op();
}

/* now for testing */
apple_module_t mod_term = {
	APPLE_MODULE_NAME,
	APPLE_MODULE_TYPE_DICT,
	NULL,	/* no init */
	NULL,	/* so no exit */
	term_start,
};

int module_term_init(void)
{
	return register_apple_module(&mod_term);
}

void module_term_exit(void)
{
	unregister_apple_module(&mod_term);
}
